<?php

use yii\grid\GridView;
use common\helpers\Html;
use yii\helpers\Url;

$this->title = '留言';
$this->params['breadcrumbs'][] = ['label' => $this->title];

?>

<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header">
                <h3 class="box-title">
                    <?= $this->title; ?>
                    <small></small>
                </h3>

            </div>
            <div class="box-body table-responsive">
                <?= GridView::widget([
                    'dataProvider' => $dataProvider,
                    'filterModel' => $searchModel,
                    // 重新定义分页样式
                    'tableOptions' => ['class' => 'table table-hover'],
                    'columns' => [
                        [
                            'class' => 'yii\grid\SerialColumn',
                        ],
                        'id',
                        'name',
                        'phone',
                        'wechat',
                        'company',
                        'type',
                        'demand',
                        [
                            'header' => "操作",
                            'class' => 'yii\grid\ActionColumn',
                            'template' => '{ajax-edit} {statue} {destroy}',
                            'buttons' => [
                                'statue' => function ($url, $model, $key) {
                                    return Html::statue($model->status);
                                },
                                'destroy' => function ($url, $model, $key) {
                                    return Html::delete(['destroy', 'id' => $model->id]);
                                },
                            ],
                        ],
                    ],
                ]); ?>
            </div>
        </div>
    </div>
    <script>
        // 启用状态 status 1:已处理;0未处理;
        function rfStatue(obj) {
            let id = $(obj).attr('data-id');
            let status = 0;
            self = $(obj);
            if (self.hasClass("btn-success")) {
                status = 1;
            }

            if (!id) {
                id = $(obj).parent().parent().attr('id');
            }

            if (!id) {
                id = $(obj).parent().parent().attr('data-key');
            }

            $.ajax({
                type: "get",
                url: "<?= Url::to(['ajax-update'])?>",
                dataType: "json",
                data: {
                    id: id,
                    status: status
                },
                success: function (data) {
                    if (parseInt(data.code) === 200) {
                        if (self.hasClass("btn-success")) {
                            self.removeClass("btn-success").addClass("btn-default");
                            self.attr("data-toggle", 'tooltip');
                            self.attr("data-original-title", '点击处理');
                            self.text('未处理');
                        } else {
                            self.removeClass("btn-default").addClass("btn-success");
                            self.attr("data-toggle", 'tooltip');
                            self.attr("data-original-title", '点击处理');
                            self.text('已处理');
                        }
                    } else {
                        rfAffirm(data.message);
                    }
                }
            });
        }
    </script>